package cn.seapon.talkerserver.view;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import cn.seapon.talkerserver.model.TalkerServer;
/**
 * 这是服务器端的控制界面，可以完成启动服务器，关闭服务器
 * 可以管理和监控用户.
 */
public class TalkerServerFrame extends JFrame implements ActionListener {

	JPanel jp1;
	JButton jb1,jb2;
	
	private boolean hasRun = false;
	
	private TalkerServer talkerServer = null;
	
	public static void main(String[] args) {

//		TalkerServer.startService();
		TalkerServerFrame mysf=new TalkerServerFrame();
	}
	
	public TalkerServerFrame(){
		jp1=new JPanel();
		jb1=new JButton("启动服务器");
		jb1.addActionListener(this);
		jb2=new JButton("关闭服务器");
		jb2.addActionListener(this);
		jp1.add(jb1);
		jp1.add(jb2);
		
		this.add(jp1);
		this.setSize(500, 400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent arg0) {
		System.out.print(arg0.getActionCommand());
		if(arg0.getSource()==jb1){
			//开启服务器
			if (!hasRun) {//未在运行
				new Thread(){

					@Override
					public void run() {
						TalkerServer.startService();
						super.run();
					}
					
				}.start();
				
				hasRun = true;
			}else {
				System.out.println("服务器已经在运行了！");
			}
		}else if (arg0.getSource() == jb2) {
			System.out.println("喝喝 ！");
			if (hasRun) {//在运行
				if (TalkerServer.stopService()) {
					System.out.println("服务已经停止！");
					hasRun = false;
				}else {
					System.out.println("停止服务失败！");
				}
			}else {
				System.out.println("服务器不在运行中！");
			}
		}else {
		}
	
	}
}
